package Arrays;

//                               数组查找
public class Test3 {
    //用一个函数查找数组指定的元素
    //顺序查找
    public static int find(int[] arr, int target){
        int i = 0;
        for (i = 0; i < arr.length; i++){
            if (arr[i] ==target){
                return i;
            }
        }
        return -1;
    }

    //二分查找
    public static int binarySearch(int[] arr, int target){
        int left = 0;
        int right = arr.length - 1;
        while (left <= right){
            int mid = (left + right) / 2;
            if (arr[mid] < target){
                left = mid + 1;
            }else if(arr[mid] > target){
                right = mid - 1;
            }
            else {
                return mid;
            }
        }
        return -1;
    }
    public static void main(String[] args) {
        int[] arr1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        int index = find(arr1,7);
        System.out.println(index);
        index = binarySearch(arr1,8);
        System.out.println(index);
    }
}
